package com.news.news.web;

import com.news.news.modle.News;
import com.news.news.modle.NewsType;
import com.news.news.modle.User;
import com.news.news.service.NewsService;
import com.news.news.service.UsersLoginService;
import com.news.news.service.UsersService;
import com.news.news.util.CommonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/**
 * Created by 罗铠威 on 2023/9/16 10:45
 * type:
 */

@RestController
@RequestMapping(path = "/userApi/UsersLoginCtl")
public class UserLoginController {

    @Resource
    UsersLoginService usersLoginService;
    @Resource
    NewsService newsService;


    /**
     * 登录用户界面
     * @param user
     */
    @RequestMapping("/login")
    public CommonResult login(@RequestBody User user) {
        User login = usersLoginService.login(user);
        if(login==null){
            return new CommonResult(201, login, "拥护账户、密码错误");

        }

        return new CommonResult(200, login, "成功");
    }


    /**
     * 用户发布新闻文章
     * @param
     */
    @RequestMapping(value = "/saveNews")
    public CommonResult saveNews(@RequestBody News news, @RequestHeader("userToken") String token) {
        if (news.getId()!= null) {//【修改】
            newsService.updateNews(news,token);
        } else {//【新增】
            usersLoginService.saveUserNews(news,token);
        }
        CommonResult commonResult = new CommonResult(200, null, "成功");

        return commonResult;
    }

    /**
     * 获取新闻类型数据列表
     * @return
     */
    @RequestMapping(value = "/findNewsTypeList")
    public CommonResult findNewsTypeList() {
        ArrayList<NewsType> newsTypes=newsService.findNewsTypeList();
        CommonResult commonResult=new CommonResult(200,newsTypes,"成功");

        return commonResult;
    }

    /**
     * 接收上传文件图片
     * @return
     */
    @RequestMapping(value = "/newsPicture")
    public CommonResult newsPicture(@RequestParam("file") CommonsMultipartFile file) throws IOException {
//        System.out.println(file.getOriginalFilename());//获取文件名字
        String fileName= new Date().getTime()%100+new Random().nextInt() +file.getOriginalFilename();//防止文件重复
        String path="D:\\Program Files\\apache-tomcat-9.0.43\\webapps\\ROOT\\newsImg/"+fileName;
        file.transferTo(new File(path));

        CommonResult commonResult = new CommonResult(200, "newsImg/"+fileName, "修改状态成功");

        return commonResult;
    }

    /**
     * 删除文件图片
     * @return
     */
    @RequestMapping(value = "/deleteImg")
    public CommonResult deleteImg(@RequestParam("imgurl") String imgurl) throws IOException {
        String url=imgurl.substring(imgurl.indexOf("88")+2);
        String path="D/Program Files/apache-tomcat-9.0.43/webapps/ROOT"+url;
        File file = new File(path);
        System.out.println(path);
        System.out.println(file.exists());
        file.delete();
        System.out.println(file.exists());
        CommonResult commonResult = new CommonResult(200, file.exists(), "修改状态成功");

        return commonResult;
    }

}
